בהתחלה קיבלתי ראיון בית שהיו שם שאלות כלליות עלי:מה הממוצע שלי,גיל וכו ובסוף שאלת תכנות שהייתי צריך לפתור תוך חצי שעה ואחרי מספר ימים היה לי ראיון טכני שבו הראו לי קוד בc והייתי צריך להגיד מה הקוד עושה ומה הבאגים בו
שאלות מתוך הראיון
שאלה בcpp: יש לך רובוט שחוצב מינרלים, הרובוט יכול לעשות כל יום אחד מהשתיים: הוא יכול לחצוב גרם מינרלים ביום שלם או לבנות כל היום רובוט חדש שיהיה זמין יום אחרי ויוכל גם לעשות אותו דבר. הייתי צריך לכתוב פונקציה שתקבל כקלט מספר מינרלים שעלי לחצוב ולהחזיר את מספר הימים המינימלי שייקח לי לחצוב את כמות המינרלים הזו.
תשובות
הוסף תשובה
|
לצפיה בתשובות
מאי 2024
that's a famous question called "The robots Mining Problem"
/*
* The mining robot problem:
*
* you need to mine x units of minerals.
* you have a robot that is able to make a single operation every day,
* it can either mine 1 units or it can build another robot like it,
* the new robot will be able to do the same starting the day after.
* Write function mine_minerals that will return the minimal days will take you to mine that amount of x units of minerals.
*/
#include
#include
#include
int work_day(int days_counter, int num_bots, int mined_units, int goal) {
// Stopping condition
if (mined_units >= goal) {
return days_counter;
}
if (days_counter > goal || num_bots > goal) {
return INT_MAX; // Use a large number to indicate a non-viable solution
}
// Every day, test option for making a bot OR mine with all bots we have.
// Make robot
int res1 = work_day(days_counter + 1, num_bots + 1, mined_units, goal);
// Mine with all bots currently exist
int res2 = work_day(days_counter + 1, num_bots, mined_units + num_bots, goal);
// Return minimum of res1 and res2
return std::min(res1, res2);
}
// x is the number of units we need to produce eventually
int mine_minerals(int x) {
// Least parallel scenario - one robot
int max_barrier = x;
// Most parallel - building x bots to mine in one day
int min_barrier = int(std::log2(x)) + 2; // Floor of days it'll take to make robots, then a day for ceiling, then a day to mine
if (max_barrier <= min_barrier) return max_barrier;
int res = work_day(0, 1, 0, x);
return res;
}
int main() {
int test_cases[] = {10, 20, 50};
for (int x : test_cases) {
std::cout << "Minimal days to mine " << x << " units: " << mine_minerals(x) << std::endl;
}
return 0;
}